package com.kevis.zhunblog.admin.interceptor;

import com.kevis.webCommon.constant.PropertiesConfigConstant;
import com.kevis.zhunblog.admin.controller.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ManageAdminUserInterceptor implements HandlerInterceptor {
    @Autowired
    PropertiesConfigConstant propertiesConfig;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //静态资源不拦截
        if (handler instanceof ResourceHttpRequestHandler) {
            return true;
        }
        String uri = request.getRequestURI();
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        boolean match = antPathMatcher.match(request.getContextPath() + "/**", uri.toLowerCase());
        //不匹配的说明需要放行，直接放行
        if (!match) {
            return true;
        }
        // 被调用的处理器对象，本质是一个方法对象，对反射中的Method对象进行了再包装，对方法进行封装加强，操作原始对象
        if (handler instanceof HandlerMethod) {
            if (((HandlerMethod) handler).getBeanType().getName().equals("org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController")) {
                return true;
            }

            Object bean = ((HandlerMethod) handler).getBean();
            if (bean instanceof BaseController) {
                //存放静态资源域名
                request.setAttribute("staticSiteDomain", propertiesConfig.staticSiteDomain);
                //这边是转换为继承的基类，拿去到相关数据，统一处理
                BaseController baseController = (BaseController) bean;
                request.setAttribute("tableTitle", baseController.pageTitle);
                request.setAttribute("moduleName", baseController.Module_Name);
                request.setAttribute("tableName", baseController.TableName);
                request.setAttribute("pageTitle", baseController.pageTitle);
                request.setAttribute("seoKeywords", baseController.seoKeywords);
                request.setAttribute("seoDescription", baseController.seoDescription);
            }
        }
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
